package cn.z2huo.usercenter.management.app.service.user;

import cn.hutool.core.bean.BeanUtil;
import cn.z2huo.usercenter.management.app.dao.user.UserDAO;
import cn.z2huo.usercenter.management.app.vo.user.UserExcelExportVO;
import cn.z2huo.usercenter.model.dataobject.user.UserDO;
import com.alibaba.excel.EasyExcel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *
 * @author z2huo
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl {

    private final UserDAO userDAO;

    /**
     * <p>使用 easyexcel 导出所有用户
     */
    public byte[] exportAllUserExcel() {
        List<UserDO> userDOS = userDAO.listAllUser();
        List<UserExcelExportVO> userExcelExportVOS = BeanUtil.copyToList(userDOS, UserExcelExportVO.class);

        byte[] bytes;
        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
            EasyExcel.write(byteArrayOutputStream).sheet("所有用户").doWrite(userExcelExportVOS);
            bytes = byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return bytes;
    }

}
